/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

/**
 * Represents the img tag.
 * @author J Patrick Davenport
 *
 */
public class Image extends ChildlessPageElement implements IInlineElement, ICoreAttributes, ILanguageAttributes<Image>, IMouseEvents<Image>, IKeyboardEvents<Image> {
	private static final String	USEMAP_KEY		= "usemap";
	private static final String	LONGDESC_KEY	= "longdesc";
	private static final String	WIDTH_KEY		= "width";
	private static final String	HEIGHT_KEY		= "height";
	private static final String	ALT_KEY			= "alt";
	private static final String	SRC_KEY			= "src";
	private static final String	IMG				= "img";

	private boolean				appendContextPath;

	/*
	 * Initializer regardless of constructor.
	 */
	{
		this.setTag(IMG);
		this.setSupportsAttributes(true);
	}

	public Image() {
	}

	/**
	 * Creates an image element, for the source added and prepends the context path.
	 * @param _src location of the image.
	 */
	public Image(final String _src, final String _alt) {
		this.setAppendContextPath(true);
		this.addLoadCallBack(new IPageElementCallback<Image>() {

			@Override
			public void execute(final Image _elementBeingLoaded) {
				_elementBeingLoaded.setSrc(_src);
				_elementBeingLoaded.setAlt(_alt);
				Image.this.setPrettyPrint(_elementBeingLoaded.isPrettyPrint());
			}
		});
	}

	/**
	 * Creates an image and allows you to flag the context path getting prepended.
	 * @param _src location to the image
	 * @param _prependContext true if you want the context path added.
	 */
	public Image(final String _src, final String _alt, final boolean _prependContext) {
		this.appendContextPath = _prependContext;
		this.addLoadCallBack(new IPageElementCallback<Image>() {

			@Override
			public void execute(final Image _elementBeingLoaded) {
				_elementBeingLoaded.setSrc(_src);
				_elementBeingLoaded.setAlt(_alt);
				Image.this.setPrettyPrint(_elementBeingLoaded.isPrettyPrint());
			}
		});
	}

	public String getSrc() {
		return this.getAttribute(SRC_KEY).getValue();
	}

	public String getAlt() {
		return this.getAttribute(ALT_KEY).getValue();
	}

	public String getHeight() {
		return this.getAttribute(HEIGHT_KEY).getValue();
	}

	public String getWidth() {
		return this.getAttribute(WIDTH_KEY).getValue();
	}

	public String getLongDesc() {
		return this.getAttribute(LONGDESC_KEY).getValue();
	}

	public String getUseMap() {
		return this.getAttribute(USEMAP_KEY).getValue();
	}

	public Image setSrc(final String _value) {
		String src = _value;
		if (this.appendContextPath) {
			src = this.getContextPath() + _value;
		}
		this.setAttribute(SRC_KEY, src);
		return this;
	}

	public Image setAlt(final String _value) {
		this.setAttribute(ALT_KEY, _value);
		return this;
	}

	public Image setHeight(final String _value) {
		this.setAttribute(HEIGHT_KEY, _value);
		return this;
	}

	public Image setWidth(final String _value) {
		this.setAttribute(WIDTH_KEY, _value);
		return this;
	}

	public Image setLongDesc(final String _value) {
		this.setAttribute(LONGDESC_KEY, _value);
		return this;
	}

	public Image setUseMap(final String _value) {
		this.setAttribute(USEMAP_KEY, _value);
		return this;
	}

	public boolean isAppendContextPath() {
		return this.appendContextPath;
	}

	public void setAppendContextPath(final boolean _appendContextPath) {
		this.appendContextPath = _appendContextPath;
	}

	@Override
	public String getCssClass() {
		return this.getAttributeValue(CSS_CLASS_KEY);
	}

	@Override
	public String getId() {
		return this.getAttributeValue(ID_KEY);
	}

	@Override
	public String getStyle() {
		return this.getAttributeValue(STYLE_KEY);
	}

	@Override
	public String getTitle() {
		return this.getAttributeValue(TITLE_KEY);
	}

	@Override
	public Image setCssClass(final String _value) {
		return (Image) this.setAttribute(CSS_CLASS_KEY, _value);
	}

	@Override
	public Image setId(final String _value) {
		return (Image) this.setAttribute(ID_KEY, _value);
	}

	@Override
	public Image setStyle(final String _value) {
		return (Image) this.setAttribute(STYLE_KEY, _value);
	}

	@Override
	public Image setTitle(final String _value) {
		return (Image) this.setAttribute(TITLE_KEY, _value);
	}

	@Override
	public Image setOnKeyDown(final String _scriptlet) {
		return (Image) this.setAttribute(KEY_DOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setOnKeyPress(final String _scriptlet) {
		return (Image) this.setAttribute(KEY_PRESS_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setOnKeyUp(final String _scriptlet) {
		return (Image) this.setAttribute(KEY_UP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnKeyDown() {
		return this.getAttributeValue(KEY_DOWN_EVENT_KEY);
	}

	@Override
	public String getOnKeyPress() {
		return this.getAttributeValue(KEY_PRESS_EVENT_KEY);
	}

	@Override
	public String getOnKeyUp() {
		return this.getAttributeValue(KEY_UP_EVENT_KEY);
	}

	@Override
	public Image setDir(final String _value) {
		return (Image) this.setAttribute(DIR_KEY, _value);
	}

	@Override
	public Image setLang(final String _lang) {
		return (Image) this.setAttribute(LANG_KEY, _lang);
	}

	@Override
	public Image setXmlLang(final String _xmlLang) {
		return (Image) this.setAttribute(XML_LANG_KEY, _xmlLang);
	}

	@Override
	public String getDir() {
		return this.getAttributeValue(DIR_KEY);
	}

	@Override
	public String getLang() {
		return this.getAttributeValue(LANG_KEY);
	}

	@Override
	public String getXmlLang() {
		return this.getAttributeValue(XML_LANG_KEY);
	}

	@Override
	public Image setOnClick(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSECLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setOnDblClick(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEDBLCLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setOnMouseDown(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEDOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setOnMouseMove(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEMOVE_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setMouseOut(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEOUT_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setMouseOver(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEOVER_EVENT_KEY, _scriptlet);
	}

	@Override
	public Image setMouseUp(final String _scriptlet) {
		return (Image) this.setAttribute(ON_MOUSEUP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnClick() {
		return this.getAttributeValue(ON_MOUSECLICK_EVENT_KEY);
	}

	@Override
	public String getOnDblClick() {
		return this.getAttributeValue(ON_MOUSEDBLCLICK_EVENT_KEY);
	}

	@Override
	public String getOnMouseDown() {
		return this.getAttributeValue(ON_MOUSEDOWN_EVENT_KEY);
	}

	@Override
	public String getOnMouseMove() {
		return this.getAttributeValue(ON_MOUSEMOVE_EVENT_KEY);
	}

	@Override
	public String getMouseOut() {
		return this.getAttributeValue(ON_MOUSEOUT_EVENT_KEY);
	}

	@Override
	public String getMouseOver() {
		return this.getAttributeValue(ON_MOUSEOVER_EVENT_KEY);
	}

	@Override
	public String getMouseUp() {
		return this.getAttributeValue(ON_MOUSEUP_EVENT_KEY);
	}
}
